//
//  ZWOriginalView.swift
//  ZWWeibo
//
//  Created by flyiron on 16/4/20.
//  Copyright © 2016年 flyiron. All rights reserved.
//

import UIKit
import SnapKit

class ZWOriginalView: UIView {
    
    var bottomCons:Constraint?
    var statusViewModel:ZWStatusesViewModel?{
        didSet{
            
            contentLabel.text = statusViewModel?.status?.text
            
            headimageView.sd_setImageWithURL(NSURL(string:(statusViewModel?.status?.user?.profile_image_url) ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            nameLabel.text = statusViewModel?.status?.user?.name
            timeLabel.text = statusViewModel?.status?.created_at
            sourceLabel.text = statusViewModel?.status?.source
            memberImageView.image = statusViewModel?.memberLevelImage
            
            bottomCons?.uninstall()
            
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                
                originalImageView.hidden = false
                
                originalImageView.snp_updateConstraints(closure: { (make) -> Void in
                    make.size.equalTo(statusViewModel?.originalPictureViewSize ?? CGSizeZero)
                })
                
                snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(originalImageView).offset(ZWStatusCellMargin).constraint
                })
                
                originalImageView.pic_urls = pic_urls
            }else{
                originalImageView.hidden = true
//                originalImageView.snp_updateConstraints(closure: { (make) -> Void in
//                    make.size.equalTo(CGSizeZero)
//                })
                snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentLabel).offset(ZWStatusCellMargin).constraint
                })
            }
        }
    }
    
//    var statusModel:ZWStatuses? {
//        didSet{
//            contentLabel.text = statusModel?.text
//            headimageView.sd_setImageWithURL(NSURL(string:(statusModel?.user?.profile_image_url) ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
//            
//        }
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(headimageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(originalImageView)
        
        //约束
        headimageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(ZWStatusCellMargin)
            make.size.equalTo(CGSize(width: ZWStatusCellHeadImageW, height: ZWStatusCellHeadImageW))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headimageView)
            make.leading.equalTo(headimageView.snp_trailing).offset(ZWStatusCellMargin)
        }
        
        memberImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(headimageView)
            make.leading.equalTo(nameLabel.snp_trailing).offset(ZWStatusCellMargin)
        }
        
        timeLabel.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(headimageView)
            make.leading.equalTo(nameLabel)
        }
        
        sourceLabel.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(ZWStatusCellMargin)
        }
        
        verifiedImageView.snp_updateConstraints { (make) -> Void in
            make.centerX.equalTo(headimageView.snp_trailing).offset(-2)
            make.centerY.equalTo(headimageView.snp_bottom).offset(-2)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headimageView.snp_bottom).offset(ZWStatusCellMargin)
            make.leading.equalTo(headimageView)
        }
        
        originalImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(ZWStatusCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
          bottomCons = make.bottom.equalTo(originalImageView.snp_bottom).offset(ZWStatusCellMargin).constraint
        }
    }
    
   
    
    private lazy var headimageView:UIImageView = UIImageView(image: UIImage(named:"avatar_default_small"))
    private lazy var nameLabel:UILabel = {
    let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZWStatusCellNameFontSize)
        label.text = "小明"
        return label
    }()
    private lazy var memberImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership") )
    private lazy var timeLabel:UILabel = {
    let label = UILabel(textColor: UIColor.orangeColor(), fontSize: ZWStatusCellTimeFontSize)
    label.text = "刚刚"
        return label
    }()
    private lazy var sourceLabel:UILabel = {
    let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZWStatusCellTimeFontSize)
        label.text = "NOKIA"
        return label
    }()
    
    private lazy var verifiedImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    private lazy var contentLabel:UILabel = {
    let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: ZWStatusCellContentFontSize,preferredMaxLayoutWidth: SCREENW - 2 * ZWStatusCellMargin)
        label.text = "asdflkjhaslkdjfhkaljsdfhlkjasdhflkjsadhfkljsadhflkajshdflkjasdhfkljashdfkljhaskldjfhaklsjdfhklajsdfhkljasdhfkjlashdfjklashdfljkhasdfkljlhaskljdfhkajlsdhflkajsdfhlaksjdfhlkajshflkajsdfh"
        return label
    }()
    private lazy var originalImageView:ZWStatusPictureView = {
        let view = ZWStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        return view
    }()
    
}
